<script src='<?php echo DIR_JS?>ui.datepicker.js' type='text/javascript' language='javascript'> </script>
<!--begin-->
<div class="section">
    <div class="section-title">Báo Cáo Khách Hàng Đặt Báo Theo Kỳ</div>  
    <?php 
    	if($_GET['print'] !="true" )
        { 
    ?>  
    <div class="right">
        <a class="button print" id="btnPrint">In</a>
        <a class="button exit" href="?route=deliman/home">Đóng</a>
    </div>
    <?php } ?>
    <div class="clearer">&nbsp;</div>
    
    <div class="section-content padding1">
    	<div>
        <?php if($_GET['print'] !="true" ){ ?>
            <form>
                <table>
                    <thead>
                        <tr class="left">
							<script language="javascript">
                                $(function() {
                                    $("#tungay").datepicker({
                                            changeMonth: true,
                                            changeYear: true,
                                            dateFormat: 'dd-mm-yy'
                                            });
                                    $("#denngay").datepicker({
                                            changeMonth: true,
                                            changeYear: true,
                                            dateFormat: 'dd-mm-yy'
                                            });
                                    });
                            </script>
                            <td><label>Ngày phát hành: từ</label></td>
                            <td><input type="text" class="text" id="tungay" name="tungay" /></td>
                            <td><label>đến:</label></td>
                            <td><input type="text" class="text" id="denngay" name="denngay" /></td>
                        </tr>
                        <tr class="clearer"><td></td></tr>
                        <tr class="left">
                            <td><label>Loại báo:</label></td>
                            <td>
                                <select name="loaibaoid" id="loaibaoid">
                                <?php 
                                    foreach($dsloaibao as $loaibao)
                                    { 
                                ?>
                                    <option value="<?php echo $loaibao['loaibaoid']?>">
                                        <?php echo $loaibao['tenloaibao']?>
                                    </option>
                                <?php 
                                    }
                                ?>
                                </select>
                            </td>
                            <td><label>Kỳ báo:</label></td>
                            <td>
                                <select name="kybaoid" id="kybaoid">
                                    <?php echo $cb ?>
                                </select>
                            </td>
                            <td>
                                <a class="button-other search" onClick="searchForm()">Tìm kiếm</a>
                            </td>
                        </tr>
                    </thead>
                </table>
            </form>
        <?php } ?>
                
            <table class="data-table" width="100%">
                <thead>
                    <tr>
                        <th><b>Tên khách hàng</b></th>
                        <th><b>Điện thoại</b></th>
                        <th><b>Email</b></th>
                        <th><b>Địa chỉ</b></th>
                        <th><b>Ngày đặt</b></th>
                        <th><b>Số lượng đặt</b></th>
                    </tr>
                </thead> 
                <tbody>
                    <?php 
                        $flag=0;
                        foreach($dskybao as $kybao)
                        {
                        	$tongcong = 0;
                    ?>
                    <tr >
                        <td colspan="4" style="background:#999; font-weight:bold" >
                            <?php
                                if(trim($kybao['tenkybao'])!='')
                                    echo $kybao['tenkybao'].' - ';
                                if(trim($kybao['ngayphathanh'])!='')
                                    echo "Ngày phát hành: ".$this->date->formatMySQLDate($kybao['ngayphathanh']);
                            ?>
                        </td>
                        <th colspan="2" style="background:#999; font-weight:bold">                            	
                            <label id="<?php echo 'sld'.$kybao['kybaoid'] ?>"></label>
                        </th>
                    </tr>
                    <?php 
                            foreach($dskhuvuc as $khuvuc)
                            {
                                $tongcong_khuvuc=0;                                
                    ?>
                    <tr  id="<?php echo 'khuvuc'.$kybao['kybaoid'].$khuvuc['khuvucid'] ?>">
                        <th colspan="4">
                            <?php
                                if(trim($khuvuc['tentinhthanh'])!='')
                                    echo $khuvuc['tentinhthanh'].' - ';
                                if(trim($khuvuc['tenquanhuyen'])!='')
                                    echo $khuvuc['tenquanhuyen'].' - ';
                                if(trim($khuvuc['tenphuongxa'])!='')
                                    echo $khuvuc['tenphuongxa'].' - ';
                                if(trim($khuvuc['tenkhuvuc'])!='')
                                    echo $khuvuc['tenkhuvuc'];
                            ?>
                        </th>
                        <th colspan="2">                                
                            <div class="clearer">&nbsp;</div>
                        </th>
                    </tr>
                    <?php 
                                foreach($dskhachhangdatbao as $khachhangdatbao)
                                {
                                    if($khachhangdatbao['khuvucid']==$khuvuc['khuvucid'] && 
                                        $khachhangdatbao['kybaoid']==$kybao['kybaoid'])
                                    {
                    ?>
                    <tr>
                        <td><?php echo $khachhangdatbao['tenkhachhang'] ?></td>
                        <td><?php echo $khachhangdatbao['dienthoai'] ?></td>
                        <td><?php echo $khachhangdatbao['email'] ?></td>
                        <td><?php echo $khachhangdatbao['diachi'] ?></td>
                        <td><div class="text-center"><?php echo $this->date->formatMySQLDate($khachhangdatbao['ngaydat']) ?></div>
                        </td>
                        <td><div class="text-center">
                                <?php 
                                        echo $this->string->numberFormate($khachhangdatbao['soluongdat']);
                                        $tongcong += (int)$khachhangdatbao['soluongdat'];
                                        $tongcong_khuvuc += (int)$khachhangdatbao['soluongdat'];
                                        $flag += $tongcong;
                                ?>
                            </div>
                    	</td>
                    </tr>
                    <?php
                                    }
                                }
                    ?>
                    <tr id="<?php echo 'tr1'.$kybao['kybaoid'].$khuvuc['khuvucid'] ?>">
                        <td colspan="5">
                            <label class="right" id="ll"><b></b> </label>
                            <label class="right"><b>Tổng cộng: </b> </label>
                        </td>
                        <td class="text number">
                        	<div class="text-center">
                            	<b><?php echo $this->string->numberFormate($tongcong_khuvuc) ?></b>
                            </div>
                        </td>
                    </tr>
                    <tr id="<?php echo 'tr2'.$kybao['kybaoid'].$khuvuc['khuvucid'] ?>">
                        <td colspan="6">
                            <label class="right" id="thongbao"><b></b> </label>
                        </td>
                    </tr>
                    <?php 
                                if($tongcong_khuvuc == 0)
                                {
                    ?>
                   	<script language="javascript">
                        $temp = "<?php echo '#khuvuc'.$kybao['kybaoid'].$khuvuc['khuvucid'] ?>";
                        $(""+$temp).hide();
                        $temp = "<?php echo '#tr1'.$kybao['kybaoid'].$khuvuc['khuvucid'] ?>";
                        $(""+$temp).hide();
                        $temp = "<?php echo '#tr2'.$kybao['kybaoid'].$khuvuc['khuvucid'] ?>";
                        $(""+$temp).hide();
                    </script>
                    <?php
                                }
                            }
                    ?>                        
                    <script language="javascript">
                        $temp = "<?php echo '#sld'.$kybao['kybaoid'] ?>";
                        $(""+$temp).text("Tổng số lượng đặt: "+"<?php echo $this->string->numberFormate($tongcong) ?>");
                    </script>
                    <?php
                        }
                        if($flag==0)
                        {
                    ?>
                    <tr>
                        <td colspan="6">
                            <label>Bạn chưa chọn loại báo cần xem hoặc loại báo bạn chọn chưa có khách hàng đặt..</label>
                        </td>
                    </tr>
                    <?php
                        }
                    ?> 
            	</tbody>
        	</table>
    	</div>
        <div class="clearer">&nbsp;</div>
    </div>
    
    <div class="section-bottom"></div>
</div>
<!--end-->

<script language="javascript">
	$("#loaibaoid").change(function(){
		$('#kybaoid').load('?route=deliman/baocaokhachhangdatbaotheoky/getKyBao&loaibaoid='+$(this).val()+'&tungay='+$('#tungay').val()+'&denngay='+$('#denngay').val(), 
		function()
		{
			$('#kybaoid').val("<?php echo $_GET['kybaoid']?>");
		});
	});
	$('#loaibaoid').val("<?php echo $_GET['loaibaoid']?>");
	$("#loaibaoid").change();
	

function searchForm()
{
	var url =  "?route=deliman/baocaokhachhangdatbaotheoky";
	if($("#loaibaoid").val() != "")
		url += "&loaibaoid="+ $("#loaibaoid").val();	
	if($("#kybaoid").val() != "")
		url += "&kybaoid="+ $("#kybaoid").val();
	if($("#tungay").val() != "")
		url += "&tungay="+ $("#tungay").val();
	if($("#denngay").val() != "")
		url += "&denngay="+ $("#denngay").val();	
	window.location = url;
}

$("#tungay").val("<?php echo $_GET['tungay']?>");
$("#denngay").val("<?php echo $_GET['denngay']?>");
$('#btnPrint').click(function(){
	
	var url = window.location+"&print=true";
	 var result = window.showModalDialog(url,"", "dialogWidth:1000px; dialogHeight:450px; center:yes");
});


</script>
<?php if($_GET['print'] =="true" ){ ?>
	<script language="javascript">
        window.print();
    </script>
<?php } ?>